"use_strict"
const fs = require('fs');

var rgx = {}

try{        //正则配置初始化
    if(fs.statSync('./config/regex.json','r')){
        LB.log.ok("正则配置加载成功！") 
    }
}catch(err){
    LB.log.busy("正则配置不存在，正在初始化...");
    fs.writeFileSync('./config/regex.json',JSON.stringify([{"regex":"^百度$","permission":1,"actions":[{"type":"http_get","content":"https://www.baidu.com"}]},{"regex":"^帮助$","permission":0,"actions":[{"type":"group_msg","content":"帮助测试"}]},{"regex":"^查服$","permission":0,"actions":[{"type":"runcmd","content":"list"}]},{"regex":"^服务器$","permission":0,"actions":[{"type":"server_info"},{"type":"server_status"}]},{"regex":"^!motdpe (.+$)","permission":0,"actions":[{"type":"motdpe"}]},{"regex":"^/cmd (.+$)","permission":0,"actions":[{"type":"runcmd_raw"}]},{"regex":"^查云黑 (xboxid|qq) (.+)$","permission":0,"actions":[{"type":"blackbe_check"}]},{"regex":"^绑定 ([A-Za-z0-9 ]{4,20})$","permission":0,"actions":[{"type":"whitelist_bind"}]},{"regex":"^申请白名单$","permission":0,"actions":[{"type":"whitelist_add_self"}]},{"regex":"^查绑定$","permission":0,"actions":[{"type":"bind_check_self"}]},{"regex":"^查绑定 (.+$)","permission":0,"actions":[{"type":"bind_check"}]},{"regex":"^加白名单 (.+$)","permission":1,"actions":[{"type":"whitelist_add"}]},{"regex":"^解绑 (.+$)","permission":1,"actions":[{"type":"whitelist_del"}]},{"regex":"^解绑$","permission":0,"actions":[{"type":"whitelist_unbind"}]}],null,'\t'));
    LB.log.ok("正则配置创建成功！");
}


rgx = JSON.parse(fs.readFileSync('./config/regex.json'))

LB.HELPER.REGEX = {}

LB.HELPER.REGEX.getRegexNum = function(msg){       //通过群消息匹配正则的索引值，匹配到则返回具体值，匹配不到返回false
    for(n in Object.values(rgx)){
        let rgxobj = new RegExp(rgx[n].regex,"g")
        LB.HELPER.REGEX.regexBody = rgxobj
        if(msg == msg.match(rgxobj)){
            return(rgx[n])
        }
    }
    return false;
}

LB.HELPER.REGEX.getRegexBody = (msg)=>{    //获取已匹配正则的正则体
    return LB.HELPER.REGEX.getRegexNum(msg).regex
}

LB.HELPER.REGEX.getRegexPermission = (msg)=>{    //获取已匹配正则的所需权限，返回0/1
    return LB.HELPER.REGEX.getRegexNum(msg).permission
}

LB.HELPER.REGEX.getRegexActions = function(msg){    //返回已匹配正则的动作内容,获取到则返回一组对象
    return LB.HELPER.REGEX.getRegexNum(msg).actions
}